package com.iflytek.management.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.iflytek.management.mapper.SysUserMapper;
import com.iflytek.management.pojo.SysUser;
import com.iflytek.management.util.MybatisUtil;

@Controller
@RequestMapping(value = "/person")
public class PersonController {
	
	@RequestMapping(value = "/showAllMember.do",method = RequestMethod.GET)
	@ResponseBody
	public JSONObject showAllMember() throws IOException {
		SysUserMapper sys_userMapper = MybatisUtil.Prepare(SysUserMapper.class);
		JSONObject res = new JSONObject();
		List<SysUser> allMember = new ArrayList<SysUser>();
		try {
			allMember = sys_userMapper.selectAll();
			
		} catch (Exception e) {
			System.out.println(e);
		}
		res.put("allUsers", allMember);
		return res;
	}
	
	@RequestMapping(value = "/toEditMember.do",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject toEditMember(@RequestBody Map<String, Object> parms, HttpSession sess) throws IOException {
		SysUserMapper sys_userMapper = MybatisUtil.Prepare(SysUserMapper.class);
		JSONObject res = new JSONObject();
		SysUser nowEdit = new SysUser();
		try {
			nowEdit = sys_userMapper.selectByPrimaryKey((Integer) parms.get("userid"));
		} catch (Exception e) {
			System.out.println(e);
			res.put("msg", "进入编辑员工失败");
		}
		sess.setAttribute("userd", nowEdit);
		res.put("msg", "进入编辑员工成功");
		return res;
	}
	
	@RequestMapping(value = "/upadteMember.do",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject upadteMember(@RequestBody SysUser user) throws IOException {
		SysUserMapper mapper = MybatisUtil.Prepare(SysUserMapper.class);
		JSONObject res = new JSONObject();
		try {
			if (user.getRoleid()!=null) {
				mapper.updatePersonByIdManager(user);
			} else {
				mapper.updatePersonById(user);
			}
			res.put("msg", "信息修改成功");
		} catch (Exception e) {
			// TODO: handle exception
			res.put("msg", "信息修改失败");
		}
		
		return res;
	}
	
	@RequestMapping(value = "/delMember.do",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject delMember(@RequestBody Map<String, Object> parms) throws IOException {
		Integer userid = (Integer) parms.get("userid");
		System.out.println(userid);
		SysUserMapper mapper = MybatisUtil.Prepare(SysUserMapper.class);
		JSONObject res = new JSONObject();
		try {
			mapper.deleteByPrimaryKey(userid);
			res.put("msg", "员工删除成功");
		} catch (Exception e) {
			// TODO: handle exception
			res.put("msg", "未知错误");
		}
		return res;
	}
	
	
	@RequestMapping(value = "/addMember.do",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject addMember(@RequestBody SysUser user) throws IOException {
		SysUserMapper mapper = MybatisUtil.Prepare(SysUserMapper.class);
		JSONObject res = new JSONObject();
		try {
			mapper.insert(user);
			res.put("msg", "员工添加成功");
		} catch (Exception e) {
			// TODO: handle exception
			res.put("msg", "未知错误");
		}
		return res;
	}
	
	
	@RequestMapping(value = "/searchUser.do",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject searchUser(@RequestBody Map<String, Object> parms) throws IOException {
		Integer userid = (Integer) parms.get("userid");

		
		SysUserMapper mapper = MybatisUtil.Prepare(SysUserMapper.class);
		SysUser user = null;
		JSONObject res = new JSONObject();
		try {
			user = mapper.selectByPrimaryKey(userid);
			if(user==null) res.put("msg", "查找员工失败");
			else res.put("searchRes", user);
		} catch (Exception e) {
			// TODO: handle exception
			res.put("msg", "未知错误");
		}
		return res;
	}
	
	
	@RequestMapping(value = "/resetPwd.do",method = RequestMethod.POST)
	@ResponseBody
	public JSONObject resetPwd(@RequestBody Map<String, Object> parms) throws IOException {
		Integer userid = (Integer) parms.get("userid");
		
		SysUserMapper mapper = MybatisUtil.Prepare(SysUserMapper.class);
		JSONObject res = new JSONObject();
		try {
			mapper.updatePwdByUserid(userid);
			res.put("msg", "重置密码成功");

		} catch (Exception e) {
			// TODO: handle exception
			res.put("msg", "未知错误");
		}
		return res;
	}
}